package com.pro.xwt.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.service.BEmployeeService;
import com.pro.xwt.common.utils.Rex;
import com.pro.xwt.common.utils.model.Authority;


public class AuthInterceptor implements HandlerInterceptor{
	
	@Autowired
	BEmployeeService bEmployeeService;
	
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}

	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
	
		BEmployee bEmployee = (BEmployee)arg0.getAttribute("user");
		//拦截路径
		String requestPath = arg0.getServletPath();
		//拦截请求方式
		String method = arg0.getMethod().toLowerCase();
		//拦截的方法名
		String pathName = method + "-" + Rex.getReplace(requestPath, "{id}");
		System.out.println(pathName);
		//拦截
		String role = bEmployee.getRole();
		System.out.println(role);
		String list = (String)Authority.getMap().get(role);
		System.out.println(list);
		if (list.indexOf(pathName)!=-1) {
			//System.out.println("拦截成功"+pathName);
			return true;
		}else{
			arg1.getWriter().write("你没有该权限");
			return false;
		}
	}

}

